package com.banggood.pss.mqtools.bean;

import com.banggood.pss.mqtools.util.ChannelCreator;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.chen
 * @date 2020/8/18 15:21
 */
public class MqBaseBean {

    protected static final Log logger = LogFactory.getLog(MqBaseBean.class);

    @Autowired
    protected ChannelCreator channelCreator;

    protected String queueName;

    protected String exchangeName;

    protected String routingKey;

    protected String dealLetterExchangeName;

    protected BuiltinExchangeType builtinExchangeType = BuiltinExchangeType.FANOUT;
    /**
     * 本变量官方不建议并发调用来发送信息。
     */
    private volatile Channel channel;

    public Channel getNewChannel() throws IOException {
        return channelCreator.getChannel();
    }

    public void setChannelCreator(ChannelCreator channelCreator) {
        this.channelCreator = channelCreator;
    }

    public String getQueueName() {
        return queueName;
    }

    /**
     * double check lock
     * @return
     */
    public Channel getChannel() {
        if(channel == null) {
            synchronized (this) {
                if(channel == null) {
                    try {
                        channel = channelCreator.getChannel();
                    } catch (IOException e) {
                        logger.info(e.getMessage(), e);
                    }
                }
            }
        }
        return channel;
    }

    public MqBaseBean(String queueName) {
        this.queueName = queueName;
    }

    public MqBaseBean(String queueName, String exchangeName) {
        this.queueName = queueName;
        this.exchangeName = exchangeName;
    }

    public MqBaseBean(String queueName, String exchangeName,String routingKey) {
        this.queueName = queueName;
        this.exchangeName = exchangeName;
        this.routingKey = routingKey;
    }

    public MqBaseBean(String queueName, String exchangeName,String routingKey, BuiltinExchangeType builtinExchangeType) {
        this.queueName = queueName;
        this.exchangeName = exchangeName;
        this.routingKey = routingKey;
        this.builtinExchangeType = builtinExchangeType;
    }

    public void createQueue() {
        try {
            if(dealLetterExchangeName != null) {
                Map<String , Object> arguments = new HashMap<>(1);
                arguments.put("x-dead-letter-exchange" , dealLetterExchangeName);
                //此参数定义消息过期时间。
                //arguments.put("x-expires", 1800000);
                //绑定死信队列
                getChannel().queueDeclare(queueName, true, false, false, arguments);
            } else {
                //绑定对应的exchange
                getChannel().queueDeclare(queueName, true, false, false, null);
            }

            if(exchangeName != null) {
                getChannel().exchangeDeclare(exchangeName, builtinExchangeType,
                        true, false, false, null);
                if(routingKey == null) {
                    routingKey = "*";
                }
                getChannel().queueBind(queueName, exchangeName, routingKey);
            }

        } catch (IOException e) {
            logger.info(e.getMessage(),e);
        }
    }

}
